# Create your views here.
from django.http import HttpResponse
from django.core import serializers
from ideas.models import Idea, Content, Connection
from django.conf.global_settings import DEFAULT_CONTENT_TYPE

def modified_node(request):
    
    # Get the new nodes
    dict_filter = lambda x, y: [y[i] for i in x]
    
    checksums = request.POST.values()
    nodes = Idea.hashes.md5()
    
    new_node_hashes = set(nodes.keys()) - set(checksums) 
    new_nodes = dict_filter(new_node_hashes, nodes)
    
    ids = sorted([n.id for n in new_nodes])
    
    return HttpResponse(','.join([str(i) for i in ids]),mimetype="text/plain")

def modified_link(request):
    
    # Get the new nodes
    dict_filter = lambda x, y: [y[i] for i in x]
    
    checksums = request.POST.values()
    
    # Get the new links
    connections = Connection.links.all()
    new_connection_hashes = set(connections.keys()) - set(checksums) 
    new_connections = dict_filter(new_connection_hashes, connections)
    
    return HttpResponse(','.join(sorted([str(n.id) for n in new_connections])),mimetype="text/plain")

def create_node(request):
    """
    title -> string rep of title
    text -> string rep of text
    parent -> id of parent
    size -> size number
    """
    print "Creating Node"
    
    #c = Content(title=request.POST['title'], text=request.POST['text'])
    #c.save()
    
    if 'parent' in request.POST:
        try:
            p = Idea.objects.get(id=request.POST['parent'])
        except ObjectDoesNotExist:
            i = Idea(title=request.POST['title'], text=request.POST['text'], size=request.POST['size'])
            i.save()
        else:
            i = Idea(parent=p, title=request.POST['title'], text=request.POST['text'], size=request.POST['size'])
            i.save()
    else:
        i = Idea(title=request.POST['title'], text=request.POST['text'], size=request.POST['size'])
        i.save()
    
    xml = serializers.serialize('xml', [i])
    
    return HttpResponse(xml,mimetype="application/xml");

    
def create_link(request):
    print request.POST['from']
    f = Idea.objects.get(id=request.POST['from'])
    print request.POST['to']
    t = Idea.objects.get(id=request.POST['to'])
    print request.POST['strength']
    stren = request.POST['strength']
    
    print f.__class__
    print t.__class__
    
    c = Connection(from_idea=f, to_idea=t, strength=str(stren))
    
    print "hi"
    c.save()
    print "hi"
    
    xml = serializers.serialize('xml', [c])
    
    print xml
    
    return HttpResponse(xml,mimetype="application/xml");

def node_property(request, node_id, node_prop):
    n = Idea.objects.get(id=node_id)
    return HttpResponse(str(n.__dict__[node_prop]), "text/plain")

def node_base(request):
    return HttpResponse(','.join(Idea.base.all().values_list('id', flat=True).order_by('id')), 'text/plain')

def link_property(request, link_id, link_prop):
    l = Connection.objects.get(id=link_id)
    return HttpResponse(str(l.__dict__[link_prop]), "text/plain")